package com.ruicar.afs.cloud.manage.contractreissur.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractVersionEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageReprintRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageReprintRecordService;
import com.ruicar.afs.cloud.manage.contractreissur.vo.ContractReissurManageVo;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HuangTao
 * @Date 2020/8/3 15:42
 * @description: 合同重出
 */
@Slf4j
@RestController
@RequestMapping("/contractReissur")
@AllArgsConstructor
public class ContractReissurController {

    private final ManageReprintRecordService manageReprintRecordService;

    private final ComPrintFormManageService comPrintFormManageService;

    private final ApproveTaskProcessor approveTaskProcessor;


    @ApiOperation(value = "查询合同重出列表")
    @PostMapping("/findContractReissueList")
    public IResponse findContractReissueList(@RequestBody ContractReissurManageVo vo) {
        vo.setProcessPerson(SecurityUtils.getUser().getUserRealName());
        Page page = new Page(vo.getPageNumber(), vo.getPageSize());
        IPage list = null;
        if (vo.getAllCase()==true){

            list = manageReprintRecordService.getReprintRecordList(page, vo);

        }else {
            list = manageReprintRecordService.getPartReprintRecordList(page, vo);
        }
        list.setRecords(approveTaskProcessor.handle(list.getRecords()));
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "合同重出保存")
    @PostMapping("/saveContractReissue")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveContractReissue(@RequestBody ContractReissurManageVo vo) {
        ContractReissurManageVo contractReissurManageVo = manageReprintRecordService.saveContractReissue(vo);
        return new IResponse<ManageReprintRecord>().setData(contractReissurManageVo);
    }

    @ApiOperation(value = "合同重出提交")
    @PostMapping("/submitContractReissue")
    @Transactional(rollbackFor = Exception.class)
    public IResponse submitContractReissue(@RequestBody ContractReissurManageVo vo) {
        manageReprintRecordService.submitContractReissue(vo);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功");
    }

    @SysLog("删除合同重出业务信息")
    @ApiOperation(value = "删除案件")
    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> delById(@PathVariable String id) {
        manageReprintRecordService.removeById(id);
        return new IResponse().setMsg("删除成功!");
    }

    @ApiOperation(value = "合同重出流程审批")
    @PostMapping("/saveProposalApplicationInfo")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveProposalApplicationInfo(@RequestBody ContractReissurManageVo vo) {
        manageReprintRecordService.saveProposalApplicationInfo(vo);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功");
    }


    @ApiOperation(value = "查询其他合同版本")
    @PostMapping("/queryContract")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryContract(@RequestBody ContractReissurManageVo vo) {
        if (vo.getContractNo() == null || vo.getContractNo() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ContractReissurManageVo> list = new ArrayList<>();
        ManageReprintRecord manageReprintRecord = manageReprintRecordService.getOne(Wrappers.<ManageReprintRecord>query().lambda()
                .eq(ManageReprintRecord::getCaseNo, vo.getCaseNo())
        );
        if (EmptyUtils.isNotEmpty(manageReprintRecord) && manageReprintRecord != null && manageReprintRecord.getContractVersion().equals(ContractVersionEnum.otherContractVersion)) {
            if (EmptyUtils.isNotEmpty(manageReprintRecord)) {
                String contractList = manageReprintRecord.getContractList();
                List<ComPrintFormManage> comPrintFormManages = JSONObject.parseArray(contractList, ComPrintFormManage.class);
                for (ComPrintFormManage comPrintFormManage : comPrintFormManages) {
                    ContractReissurManageVo contractReissurManageVo = new ContractReissurManageVo();
                    contractReissurManageVo.setPrintFormName(comPrintFormManage.getPrintFormName());
                    List<ComPrintFormManage> comPrintFormManageList = new ArrayList<>();
                    comPrintFormManageList.add(comPrintFormManage);
                    contractReissurManageVo.setVersionList(comPrintFormManageList);
                    list.add(contractReissurManageVo);
                }
                vo.setVersionList(comPrintFormManages);
            }

        } else {
            return new IResponse().setData(list);
        }

        return new IResponse().setData(list);
    }


    @ApiOperation(value = "查询激活合同")
    @PostMapping("/queryNewContract")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryNewContract(@RequestBody ContractReissurManageVo vo) {

        if (vo.getContractNo() == null || vo.getContractNo() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ContractReissurManageVo> list = manageReprintRecordService.queryNewContract(vo);
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "查询业务办理的合同")
    @PostMapping("/queryBusinessContract")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryBusinessContract(@RequestBody ContractReissurManageVo vo) {

        if (vo.getContractNo() == null || vo.getContractNo() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ContractReissurManageVo> list = new ArrayList<>();;
        ManageReprintRecord manageReprintRecord = manageReprintRecordService.getOne(Wrappers.<ManageReprintRecord>query().lambda()
                .eq(ManageReprintRecord::getCaseNo, vo.getCaseNo())
        );
        if (EmptyUtils.isNotEmpty(manageReprintRecord) && manageReprintRecord != null && manageReprintRecord.getContractVersion().equals(ContractVersionEnum.newContractVersion)) {
            if (EmptyUtils.isNotEmpty(manageReprintRecord)) {
                String contractList = manageReprintRecord.getContractList();
                List<ComPrintFormManage> comPrintFormManages = JSONObject.parseArray(contractList, ComPrintFormManage.class);
                for (ComPrintFormManage comPrintFormManage : comPrintFormManages) {
                    ContractReissurManageVo contractReissurManageVo = new ContractReissurManageVo();
                    contractReissurManageVo.setPrintFormName(comPrintFormManage.getPrintFormName());
                    List<ComPrintFormManage> comPrintFormManageList = new ArrayList<>();
                    comPrintFormManageList.add(comPrintFormManage);
                    contractReissurManageVo.setVersionList(comPrintFormManageList);
                    list.add(contractReissurManageVo);
                }
                vo.setVersionList(comPrintFormManages);
            }
        } else {
            list = new ArrayList<>();
        }

        return new IResponse().setData(list);
    }

    @ApiOperation(value = "查询该用户所有的合同")
    @PostMapping("/queryAllNewContract")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryAllNewContract(@RequestBody ContractReissurManageVo vo) {

        if (vo.getBusiNo() == null || vo.getBusiNo() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        if (vo.getPrintNode() == null || vo.getPrintNode() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        if (vo.getPrintNode() == null || vo.getPrintNode() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ContractReissurManageVo> contractReissurManageVo = manageReprintRecordService.addNewContract(vo);
        return new IResponse().setData(contractReissurManageVo);
    }

    @ApiOperation(value = "批量下载合同")
    @PostMapping("/downContractList")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> printContractList(HttpServletResponse response, @RequestBody ContractReissurManageVo vo) {
        boolean b = manageReprintRecordService.downContractList(response, vo);
        if (b = true) {
            return new IResponse().setMsg("正在下载中!");
        } else {
            throw new AfsBaseException(CommonConstants.FAIL, "下载失败！");
        }


    }

    @ApiOperation(value = "根據场景查询所有的合同")
    @PostMapping("/queryContractByScene")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryContractByScene(@RequestBody ContractReissurManageVo vo) {
        vo.setBusiNo("");
        if (vo.getBusiNo() != "") {
            throw new AfsBaseException(CommonConstants.FAIL, "传入参数错误！");
        }

        if (vo.getPrintNode() == null && vo.getPrintNode() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ContractReissurManageVo> list = manageReprintRecordService.queryContractByScene(vo);

        return new IResponse().setData(list);
    }

    @ApiOperation(value = "根据合同名称查询合同版本")
    @PostMapping("/queryContractByName")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ContractReissurManageVo> queryContractByName(@RequestBody ContractReissurManageVo vo) {
        vo.setBusiNo("");
        if (vo.getBusiNo() != "") {
            throw new AfsBaseException(CommonConstants.FAIL, "传入参数错误！");
        }
        if (vo.getPrintFormName() == null && vo.getPrintFormName() == "") {
            throw new AfsBaseException(CommonConstants.FAIL, "请传入参数！");
        }
        List<ComPrintFormManage> list = comPrintFormManageService.list(
                Wrappers.<ComPrintFormManage>query().lambda()
                        .eq(ComPrintFormManage::getPrintNode, vo.getPrintNode())
                        .eq(ComPrintFormManage::getPrintFormName, vo.getPrintFormName())
        );
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "添加合同版本")
    @PostMapping("/addContractVersion")
    @Transactional(rollbackFor = Exception.class)
    public IResponse addContractVersion(@RequestBody ContractReissurManageVo vo) {

        return null;
    }

    @ApiOperation(value = "添加合同版本")
    @PostMapping("/addContractFile")
    @Transactional(rollbackFor = Exception.class)
    public IResponse addContractFile(@RequestBody ContractReissurManageVo vo) {

        return new IResponse<Boolean>().setData(true).setMsg("保存成功");
    }
    @ApiOperation(value = "根据案件号查询合同重出信息")
    @PostMapping("/queryContractInfo")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ManageReprintRecord> queryContractInfo(@RequestBody ContractReissurManageVo vo) {
        ManageReprintRecord one = manageReprintRecordService.getOne(Wrappers.<ManageReprintRecord>query().lambda().eq(ManageReprintRecord::getCaseNo, vo.getCaseNo()));
        if (EmptyUtils.isNotEmpty(one)) {
            one = new ManageReprintRecord();
        }
        return new IResponse<ManageReprintRecord>().setData(one);
    }
}


